# -*- coding: utf-8 -*-

# @File    : unit.py
# @Date    : 2022-06-23
# @Author  : luoyulong
import json
import subprocess
import sys
from rich.console import Console
from rich.panel import Panel
from rich.style import Style
from rich.table import Table
from rich.progress import track, Progress, SpinnerColumn, TextColumn, BarColumn, TimeElapsedColumn


def get_cookies(file_name):
    with open(f"saved_cookies\\{file_name}.txt") as f:
        cookies = f.read()
        cookies = {x['name']: x['value'] for x in json.loads(cookies)}
        return cookies


console = Console()


def progress_bar(total_percent,sessionName):
    _progress = Progress(TextColumn("[bold blue][progress.description]{task.description}"),
                         SpinnerColumn(finished_text="😊"),
                         BarColumn(style=Style(color="dark_goldenrod"),complete_style=Style(color="green4")),
                         TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),)
    _bar = _progress.add_task(description=f"{sessionName[:10]}.", total=total_percent)
    return _progress, _bar


def install_requirements():
    try:
        with open("requirements.txt", "r") as f:
            pass
    except FileNotFoundError:
        print("requirements.txt not found. Skipping installation.")
        return
    try:
        with open("requirements.txt", "r",encoding='utf-8') as f:
            packages = f.read().splitlines()

        for package in packages:
            if package.strip():
                try:
                    subprocess.check_call([sys.executable, "-m", "pip", "install", package])
                except subprocess.CalledProcessError:
                    print(f"Failed to install {package}")
    except Exception as e:
        pass


if __name__ == "__main__":
    pass
